//
//  BubbleTableView.m
//  Saas
//
//  Created by 黄志刚 on 2020/3/12.
//  Copyright © 2020 bing. All rights reserved.
//

#import "BubbleTableView.h"

@interface BubbleTableView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,copy) BubbleCellClickBlock cellClickBlock;
@property (nonatomic,copy) NSArray <NSString *>*dataArray;
@property (nonatomic,copy) NSArray *fromDataArray;


@end

@implementation BubbleTableView

- (instancetype)initWithLocation:(BubbleLocation)location
                 bubbleAlignment:(BubbleAlignment)alignment
                           width:(CGFloat)width
                          height:(CGFloat)height
                  cellClickBlock:(BubbleCellClickBlock)cellClickBlock
                       hideBlock:(BubbleHideBlock)hideBlock {
    self = [super initWithLocation:location bubbleAlignment:alignment width:width height:height hideBlock:hideBlock];
    
    self.cellClickBlock = cellClickBlock;
    
    self.bubbleCellHeight = 44;
    self.isBubbleScrollEnabled = YES;
    self.bubbleCellTextAlignment = NSTextAlignmentLeft;
    self.bubbleCellTextFont = kFont(16);
    self.selectRow = -1;
    
    return self;
    
}
- (void)showWithTargetView:(UIView *)targetView
             maskSuperView:(UIView *)maskSuperView {
    
    [super showWithTargetView:targetView maskSuperView:maskSuperView];
    
    
    self.tableView.width = self.mainView.width;
    self.tableView.height = self.mainView.height;
    [self.mainView addSubview:self.tableView];
    self.tableView.separatorColor = kColor(230, 230, 230, 1);
    [self.tableView reloadData];
}
- (void)hide {
    [super hide];
}
- (void)updateBubble:(NSArray <NSString *>*)dataArray {
    self.dataArray = dataArray;
    [self.tableView reloadData];
}
- (void)updateBubble:(NSArray <NSString *>*)dataArray fromDataArray:(NSArray *)fromDataArray {
    self.dataArray = dataArray;
    self.fromDataArray = fromDataArray;
    [self.tableView reloadData];
}
- (void)updateBubbleWithLeftArray:(NSArray <NSString *>*)leftArray rightArrray:(NSArray <NSString *>*)rightArray fromDataArray:(NSArray *)fromDataArray {
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 0; i < leftArray.count; i++) {
        NSString *key = leftArray[i];
        NSString *value = @"";
        if (rightArray.count > i) {
            value = rightArray[i];
        }
        [array addObject:@{@"key":key,@"value":value}];
    }
    self.dataArray = array;
    self.fromDataArray = fromDataArray;
    [self.tableView reloadData];
}

- (void)setBubbleCellHeight:(CGFloat)bubbleCellHeight {
    _bubbleCellHeight = bubbleCellHeight;
    [self.tableView reloadData];
}
- (void)setIsBubbleScrollEnabled:(BOOL)isBubbleScrollEnabled {
    _isBubbleScrollEnabled = isBubbleScrollEnabled;
    self.tableView.scrollEnabled = isBubbleScrollEnabled;
}
- (void)setBubbleCellTextAlignment:(NSTextAlignment)bubbleCellTextAlignment {
    _bubbleCellTextAlignment = bubbleCellTextAlignment;
    [self.tableView reloadData];
}
- (void)setBubbleCellTextFont:(UIFont *)bubbleCellTextFont {
    _bubbleCellTextFont = bubbleCellTextFont;
    [self.tableView reloadData];
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.mainView.width, self.mainView.height) style:(UITableViewStylePlain)];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.tableFooterView = [[UIView alloc] init];// 去掉多余的线条
        // 使分割线对齐
        if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins: UIEdgeInsetsMake(0, 0, 0, 0)];
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(void)setSelectRow:(NSInteger)selectRow {
    _selectRow = selectRow;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *identifier = @"cellID";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
//    }
//
//    NSString *title = self.dataArray[indexPath.row];
//    cell.textLabel.text = title;
//    cell.textLabel.textAlignment = self.bubbleCellTextAlignment;
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    
    static NSString *identifier = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    id item = self.dataArray[indexPath.row];
    if ([item isKindOfClass:[NSString class]]) {
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
        }
        cell.textLabel.text = item;
        cell.textLabel.textAlignment = self.bubbleCellTextAlignment;
         cell.textLabel.font = self.bubbleCellTextFont;
    }
    
    if ([item isKindOfClass:[NSDictionary class]]) {
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:identifier];
        }
        NSDictionary *itemDic = item;
        cell.textLabel.text = itemDic[@"key"];
        cell.detailTextLabel.text = itemDic[@"value"];;
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.selectRow == -1) {
        cell.textLabel.textColor = kBlackColor;
    }else if (indexPath.row == self.selectRow) {
        cell.textLabel.textColor = kBuleColor;
    }else {
        cell.textLabel.textColor = kBlackColor;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.bubbleCellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.cellClickBlock) {
        self.cellClickBlock(indexPath.row,self.dataArray[indexPath.row],self.fromDataArray);
        [self hide];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void) {
//           [self hide];
//        });
    }
}



@end
